<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use YangJiSen\CacheUserProvider\CacheForget;

class UserBlackCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user-black';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        \Log::info('-----------start-user-black-----------------------------');
        User::where('is_black', 1)->chunkById(50, function ($list) {
            $start_at = date("Y-m-d",strtotime("-7 day"));
             foreach ($list as $user) {
                 if (strtotime($user->black_at) > strtotime($start_at)) {
                    $user->is_black = 0;
                    $user->black_at = null;
                    $user->save();
                     CacheForget::CacheForget($user);
                 }
             }
        });
        \Log::info('-----------end-user-black-----------------------------');
    }
}
